<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*员工管理*/
class Staff extends AdminBase{
	private $user;	
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		$this->user=new \app\admin\model\User();
		parent::_initialize();  
    }
	function index(){
		echo $this->fetch();
	}
	/**
	 * 新增员工
	 */	
	function add(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$count=db("Interface")->where("card_no='$card_no'")->count(1);
			if($count>0){
				$this->ajax_error("卡号已存在");
			}
		   $data=array(
					"card_no"=>$card_no,
					"in_varifycation"=>input("in_varifycation"),
					"in_varifycation_type"=>input("in_varifycation_type"),
					"mobile"=>input("mobile"),
					"name"=>input("name"),
					"descs"=>input("descs"),
					"owner_type"=>2
			);
			if(input("in_varifycation")==1){//需要验证
				if(input("in_varifycation_type")==1){//指纹验证
					$data['fingerprint']=input("fingerprint");
				}else if(input("in_varifycation_type")==2){//人脸验证
					 $this->_face_code_insertOrupdate(array(
							  							"card_no"=>$card_no,"face1"=>input("face1"),
							  							"face2"=>input("face2"),"face3"=>input("face3"),
							  							"icon1"=>input("icon1"),"icon2"=>input("icon2"),
							  							"icon3"=>input("icon3"),
							  							));
				}	
			}else{
				$data['in_varifycation_type']=0;
			}
			
			db("Interface")->insert($data);
		   return $this->ajax_success("成功");
		}else{
			echo $this->fetch();
		}
	}
	
    /**
	 * 修改员工
	 */
	function edit(){
		if(request()->isPost()){
		   $model=db("Interface")->find(input("id"));
		   $model["in_varifycation"]=input("in_varifycation");
		   $model["mobile"]=input("mobile");
		   $model["name"]=input("name");
		   $model["descs"]=input("descs");
		  
		   if(input("in_varifycation")==1){//需要验证
				if(input("in_varifycation_type")==1){//指纹验证
					$model['fingerprint']=input("fingerprint");
			  	    $model['in_varifycation_type']=1;
				}else if(input("in_varifycation_type")==2){//人脸验证
					 $this->_face_code_insertOrupdate(array(
							  							"card_no"=>$model['card_no'],"face1"=>input("face1"),
							  							"face2"=>input("face2"),"face3"=>input("face3"),
							  							"icon1"=>input("icon1"),"icon2"=>input("icon2"),
							  							"icon3"=>input("icon3"),
							  							));
					$model['in_varifycation_type']=2;									
				}	
			}else{
				$model['in_varifycation_type']=0;
			}
			
			db("Interface")->update($model);
		   return $this->ajax_success("成功");
		
		}else{
			$model=db("Interface")->find(input("id"));
			$this->assign("model",$model);
			$facecode=db("FaceCode")->where("card_no='".$model['card_no']."'")->find();
			if(empty($facecode)){
				$this->assign("is_bind_face",0);
				$facecode=array("face1"=>null,"face2"=>null,"face3"=>null,"icon1"=>null,"icon2"=>null,"icon3"=>null);
			}else{
				$this->assign("is_bind_face",1);
			}
			if(empty($model['fingerprint'])){
				$this->assign("is_bind_finger",0);
			}else{
				$this->assign("is_bind_finger",1);
			}
			
			$this->assign("facecode",$facecode);
			echo $this->fetch();
		}
	}
	
	/**
	 * 删除员工 
	 **/
	function delete(){
		$id=input("id");
		$model=db("Interface")->find($id);
		$card_no=$model['card_no'];
		$count=db("Interface")->where("original_id=$id")->count(1);
		if($count>0){// 存在借卡
		    $this->ajax_error("此员工借卡未还，请先还卡");	
		}
		//清理卡
		db("Interface")->delete($id);
		//清理人脸数据
		db("FaceCode")->where("card_no='$card_no'")->delete();
		
		$this->ajax_success("成功");
	}
	
	/*员工卡借卡*/
	function  borrow(){
		if(request()->isPost()){
			$card_no=input("card_no");//借的卡号
			$count=db("Interface")->where("card_no='$card_no'")->count(1);
			if($count>0){
				return $this->ajax_error("此卡号已经存在");
			}
			//新增一条借记卡
			db("Interface")->insert(array("card_no"=>input("card_no"),"name"=>input("name"),"mobile"=>input("mobile"),"is_original"=>0,"owner_type"=>2,"descs"=>input("descs"),"original_id"=>input("id")));
			//新增一条记录
			db("BorrowCard")->insert(array("card_no"=>input("card_no"),"name"=>input("name"),"mobile"=>input("mobile"),"descs"=>input("descs"),"original_card_no"=>input("original_card_no"),"owner_type"=>2));
			return $this->ajax_success("成功");
		}else{
			$id=input('id');
			$count=db("Interface")->where("original_id=$id")->count(1);
			if($count>0){// 存在借卡
			    echo "此员工已经借卡一张，请先归还";	
			    die;
			}
			$model=db("Interface")->find($id);
			$this->assign("model",$model);
			echo $this->fetch();	
		}
	}
	
	/*员工还卡*/
	function  back(){
		if(request()->isPost()){
			$card_no=input("card_no");//借的卡号
			//修改借记状态
			db("BorrowCard")->where("card_no='$card_no' and is_back=0 ")->update(array("back_time"=>date("Y-m-d H:i:s"),"is_back"=>1));
			//删除借记卡
			db("Interface")->where("card_no='$card_no' ")->delete();
			return $this->ajax_success("成功");
		}else{
			$id=input('id');
			//借记的卡号
			$borrow_card_no=db("Interface")->where("original_id=$id")->value("card_no");
			if(empty($borrow_card_no)){
			    echo "此员工没有借卡，无须还卡";	
			    die;
			}
			$this->assign("borrow_card_no",$borrow_card_no);
			$model=db("Interface")->find($id);
			$this->assign("model",$model);
			echo $this->fetch();	
		}
	}
	/**
	 * 员工换卡 
	 * */
	 function change_card(){
		if(request()->isPost()){
			$card_no=input("card_no");//新的卡号
			$old_card=input("old_card");//旧的卡号
			$id=input("id");
			
			$count=db("Interface")->where("card_no='$card_no'")->count(1);
			if($count>0){
				return $this->ajax_error("新卡号已经存在");
			}
			//修改卡号
			db("Interface")->where("id=$id ")->setField(array("card_no"=>$card_no));
			//修改人脸模板卡号
			db("FaceCode")->where("card_no='$old_card'")->fetchsql(FALSE)->setField(array("card_no"=>$card_no));
			return $this->ajax_success("成功");
		}else{
			$id=input('id');
			$count=db("Interface")->where("original_id=$id")->count(1);
			if($count>0){// 存在借卡
			    echo "此员工已经借卡一张，请先归还";	
			    die;
			}
			
			$model=db("Interface")->find($id);
			$this->assign("model",$model);
			echo $this->fetch();	
		}
	}
	 
	/*员工列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041000");
		$this->assign("menus",$auth_group);
	}
	
	/*获取员工数据*/
	private function _ajax_index(){
		$query=array('name'=>array("field" =>"name","operator" =>"like"),
					 'card_no'=>array("field" =>"card_no","operator" =>"like"),
					 'mobile'=>array("field" =>"mobile","operator" =>"like"),
					 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and owner_type=2 and is_original=1 ";
		
		//借计卡条件
		if(!empty(input("borrow_card_no"))){
			$original_id=db("Interface")->where(" is_original=0 and owner_type=2 and card_no='".input("borrow_card_no")."'")->value("original_id");
			if(empty($original_id)){
				$original_id=0;
			}
			$where_and.=" and id=".$original_id;
		}
		
		$total=db("Interface")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("Interface")->field("fingerprint",true)->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		$borrow_list=$this->borrow_list();
		foreach ($list as $key => $value) {
			if(isset($borrow_list[$value['id']])){
				$list[$key]["borrow_card_no"]=$borrow_list[$value['id']];
			}else{
				$list[$key]["borrow_card_no"]="";
			}
		}
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	//插入或者修改facecode
	private function _face_code_insertOrupdate($data){
		if(empty($data)){
			return;
		}else{
			$card_no=$data["card_no"];
			$model=db("FaceCode")->where("card_no='$card_no'")->find();
			if(empty($model)){//新增
				db("FaceCode")->insert($data);
			}else{//修改
				db("FaceCode")->update(array_merge($model,$data));
			}
			return;
		}
		
	}
	//获取员工借卡列表
	public function borrow_list(){
		return db("Interface")->where("owner_type=2 and is_original=0")->column("original_id,card_no");
	}
	
}
